<template>
  <div class="app-container">
    <div class="header">
      <div class="word">
        <div class="w1">
          订单管理系统
        </div>
        <div class="w2">
          在订单管理系统中你可以查看各个用户的订单信息，同时还可以实现修改用户的订单信息，添加、删除订单等功能。
        </div>
      </div>
      <img src="../../static/u=2179590687,2836296523&fm=253&fmt=auto&app=138&f=JPEG.webp" class="imgDri">
    </div>
    <div class="index">
      <div class="searchHead">
        <el-input v-model="input3" placeholder="请输入要查询的用户的id" class="searchIuput">
          <el-button slot="append" v-model="searchInf" icon="el-icon-search" @click="searchMes" />
        </el-input>
      </div>

      <el-table :data="mes" style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-table :data="mes[props.$index].orderItemsList" border>
              <el-table-column label="加工溯源ID" prop="protTraceId" align="center" />
              <el-table-column label="产品ID" prop="productId" align="center" />
              <el-table-column label="产品名" prop="productName" align="center" width="120" />
              <el-table-column label="价格" prop="productPrice" align="center" width="75" />
              <el-table-column label="数量" prop="productNumber" align="center" width="75" />
              <el-table-column label="详情" prop="productDesc" align="center" />

              <el-table-column label="图片" prop="productPicture" align="center" width="130">
                <template scope="scope">
                  <el-image style="width: 100px; height: 100px" :src="scope.row.productPicture"></el-image>
                </template>
              </el-table-column>
            </el-table>
          </template>
        </el-table-column>
        <el-table-column label="收货人" prop="receiveName">
        </el-table-column>
        <el-table-column label="地址" prop="address">
        </el-table-column>
        <el-table-column label="状态" prop="status">
        </el-table-column>
        <el-table-column label="邮政编码" prop="postalCode">
        </el-table-column>
        <el-table-column label="电话号码" prop="phone">
        </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="changeMsg(mes[scope.$index],scope.$index)">修改状态</el-button>
            <el-button type="text" style="color:red" @click="mes[scope.$index].isShowDel = true">删除</el-button>

            <el-dialog :modal-append-to-body="false" title="提示" :visible.sync="mes[scope.$index].isShowDel" width="30%">
              <span>确认要删除此条信息吗？</span>
              <span slot="footer" class="dialog-footer">
                <el-button @click="isShowDel = false">取 消</el-button>
                <el-button type="primary" @click="driveDel(scope.$index)">确 定</el-button>
              </span>
            </el-dialog>

            <el-dialog
              :modal-append-to-body="false"
              title="修改信息"
              :visible.sync="mes[scope.$index].isShow"
              :before-close="handleClose"
            >
              <el-form class="form">
                <el-form-item class="item" label="订单id" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].orderId" autocomplete="off" disabled />
                </el-form-item>
                <el-form-item class="item" label="用户id" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].userId" autocomplete="off" disabled />
                </el-form-item>
                <el-form-item class="item" label="产品名" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].productName" autocomplete="off" disabled />
                </el-form-item>
                <el-form-item class="item" label="价格" :label-width="formLabelWidth" disabled>
                  <el-input v-model="mes[scope.$index].productPrice" autocomplete="off" disabled />
                </el-form-item>
                <el-form-item class="item" label="数量" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].productNumber" autocomplete="off" disabled />
                </el-form-item>
                <el-form-item class="item" label="地址" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].address" class="in" autocomplete="off" disabled />
                </el-form-item>
                <el-form-item class="item" label="邮政编码" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].postalCode" class="in" autocomplete="off" disabled />
                </el-form-item>
                <el-form-item class="item" label="收货人" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].receiveName" class="in" autocomplete="off" disabled />
                </el-form-item>
                <el-form-item class="item" label="电话号码" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].phone" class="in" autocomplete="off" disabled />
                </el-form-item>
                <div style="margin-left:10px;height: 61px;">
                  <label
                    style="display:inline-block;height: 36px;line-height: 36px;padding: 0 12px 0 0;width: 110px;text-align: right;"
                  >状态</label>
                  <el-select
                    v-model="mes[scope.$index].status"
                    class="sel2"
                    placeholder="请选择"
                    style="width: 186px;"
                    :disabled="mes[scope.$index].status=='已销售'?true:false"
                  >
                    <el-option label="支付失败" value="支付失败" />
                    <el-option label="支付成功" value="支付成功" />
                    <el-option label="已付款" value="已付款" />
                    <el-option label="发货" value="运输中" />
                  </el-select>
                </div>
                <el-form-item class="item" label="头像图片地址" :label-width="formLabelWidth">
                  <el-input
                    v-model="mes[scope.$index].productPicture"
                    autocomplete="off"
                    type="textarea"
                    disabled
                    style="width: 350px;"
                  />
                </el-form-item>
              </el-form>

              <div slot="footer" class="dialog-footer">
                <el-button @click="cancelChangeMsg(scope.$index)">取 消</el-button>
                <el-button type="primary" @click="changeInspectionForm(scope.$index,mes[scope.$index])">确 定</el-button>
              </div>

            </el-dialog>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          :page-size="5"
          layout="prev, pager, next, jumper"
          :total="pageSize"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script>

export default {
  name: 'FarmlandSelection',
  data() {
    return {
      mes: [],
      firstMes: [],
      allMes: [],
      index: null,
      dialogTableVisible: false,
      formLabelWidth: '120px',
      addMes: {},
      dialogFormVisible: false,
      outerVisible: false,
      pageSize: null,
      pageNum: 1,
      val: 1,
      deliverPageNum: 1,
      deliverPageSize: null,
      select: '',
      searchInf: '',
      input3: '',
      total: null,
      allEmployee: [],
      allProduct: [],
      allCompany: [],
      ShowDel: false
    }
  },
  async mounted() {
    this.getDrive()
  },
  methods: {
    async getDrive() {
      var a = JSON.stringify({})
      console.log(a)
      await request({
        method: 'get',
        url: 'mall/order/pageOrder/' + this.val + '/5'
      }).then((response) => {
        console.log(response, 'getAll')
        this.mes = response.data.records
        for (let i = 0; i < this.mes.length; i++) {
          this.$set(this.mes[i], 'isShow', false)
          this.$set(this.mes[i], 'isShowDel', false)
        }
        this.pageSize = response.data.total
        this.total = response.data.total
      })
    },
    changeMsg(mesIndex, index) {
      console.log(index, 'index')
      mesIndex.isShow = true
      this.firstMes = JSON.parse(JSON.stringify(this.mes))[index]
      this.index = index
      console.log(this.mes[index], 'mesIndex')
    },

    async changeInspectionForm(index, mes) {
      var changMes = JSON.parse(JSON.stringify(this.mes))[index]
      console.log(mes, index, 'changMes000')
      console.log(this.firstMes.status, '11111', changMes.status)
      if (this.firstMes.status != '运输中' && this.firstMes.status != changMes.status && changMes.status == '运输中') {
        await request({
          method: 'put',
          url: '/mall/order/updateToTranSport/' + this.firstMes.orderId
        }).then(response => {
          this.getDrive()
          if (response.statusCode == 20000) {
            this.$message({
              message: '修改成功',
              type: 'success'
            })
          } else {
            this.$message({
              message: '发生错误，修改失败',
              type: 'warning'
            })
          }
          this.mes[index].isShow = false
        })
      } else if (this.firstMes.status != '运输中' && this.firstMes.status != changMes.status && changMes.status == '已付款') {
        await request({
          method: 'put',
          url: '/mall/order/updateToPayOk/' + this.firstMes.orderId
        }).then(response => {
          this.getDrive()
          if (response.statusCode == 20000) {
            this.$message({
              message: '修改成功',
              type: 'success'
            })
          } else {
            this.$message({
              message: '发生错误，修改失败',
              type: 'warning'
            })
          }
          this.mes[index].isShow = false
        })
      } else if (this.firstMes.status != '运输中' && this.firstMes.status != changMes.status && changMes.status == '交易失败') {
        await request({
          method: 'put',
          url: '/mall/order/updateToTranNOT/' + this.firstMes.orderId
        }).then(response => {
          this.getDrive()
          if (response.statusCode == 20000) {
            this.$message({
              message: '修改成功',
              type: 'success'
            })
          } else {
            this.$message({
              message: '发生错误，修改失败',
              type: 'warning'
            })
          }
          this.mes[index].isShow = false
        })
      } else if (this.firstMes.status != '运输中' && this.firstMes.status != changMes.status && changMes.status == '交易成功') {
        await request({
          method: 'put',
          url: '/mall/order/updateToTranOK/' + this.firstMes.orderId
        }).then(response => {
          this.getDrive()
          if (response.statusCode == 20000) {
            this.$message({
              message: '修改成功',
              type: 'success'
            })
          } else {
            this.$message({
              message: '发生错误，修改失败',
              type: 'warning'
            })
          }
          this.mes[index].isShow = false
        })
      } else if (this.firstMes.status == '运输中' && this.firstMes.status != changMes.status) {
        this.$message({
          message: '此商品已销售，不能再更改',
          type: 'warning'
        })
      }
    },
    cancelChangeMsg(index) {
      this.mes[index].isShow = false
      this.mes[index] = JSON.parse(JSON.stringify(this.firstMes))
      this.getDrive()
    },
    async driveDel(index) {
      await request({
        method: 'delete',
        url: '/mall/order/deleteById/' + this.mes[index].orderId
      }).then(response => {
        this.val = 1
        this.getDrive()
        if (response.statusCode == 20000) {
          this.dialogFormVisible = false
          this.$message({
            message: '删除成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '发生错误，删除失败',
            type: 'warning'
          })
        }
      })
      this.mes[index].isShowDel = false
    },

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      this.val = val
      request({
        method: 'get',
        url: 'mall/order/pageOrder/' + val + '/5'
      }).then((response) => {
        this.mes = response.data.records
        for (let i = 0; i < this.mes.length; i++) {
          this.$set(this.mes[i], 'isShow', false)
          this.$set(this.mes[i], 'isShowDel', false)
        }
      })
    },

    searchMes() {
      if (this.input3) {
        request({
          method: 'get',
          url: '/mall/order/pageCollectionByUserId/' + this.input3 + '/1/5'
        }).then((response) => {
          console.log(response.data, 'searchMes')
          if (response.statusCode == 20000) {
            this.$message({
              message: '查询成功',
              type: 'success'
            })
            this.mes = []
            this.mes = response.data.records
          } else {
            this.$message({
              message: '查无该用户，查询失败',
              type: 'warning'
            })
          }
        })
      } else {
        this.getDrive()
      }
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
          this.addMes = {}
          this.cancelChangeMsg(this.index)
        })
        .catch(_ => { })
    }
  }
}
</script>

<style lang="scss" scoped>
.header {
  width: 100%;
  height: 350px;
  display: flex;
  margin-bottom: 20px;
  justify-content: space-between;
}

.form {
  display: flex;
  flex-wrap: wrap;
}

.sel {
  width: 360px;
  display: flex;
  height: 50px;
  justify-content: flex-end;
  align-content: center;
}

.sel2 {
  width: 240px;
}

.page {
  width: 100%;
  display: flex;
  justify-content: right;
}

.imgDri {
  height: 300px;
  width: 400px;
}

.w1 {
  height: 80px;
  font-size: 25px;
  font-weight: 350;
}

.w2 {
  height: 50px;
  font-size: 15px;
  font-weight: 250;
}

.searchHead {
  display: flex;
  justify-content: space-between;
}

.searchIuput {
  width: 350px;
}

.sel3 {
  width: 120px;
}
</style>

